package controller;

import bean.Users;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String telephone = req.getParameter("telephone");
        String name = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String sex = req.getParameter("sex");

        Users users = new Users();
        users.setName(name);
        users.setTel(telephone);
        users.setEmail(email);
        users.setPwd(password);
        users.setSex(sex);

        resp.setContentType("text/html;charset=utf-8");
        int result=UserService.register(users);
        if(result==1){
            req.getSession().setAttribute("user", users);
            resp.sendRedirect("HomeController");
        }else if(result==0){
            resp.getWriter().write("用户已存在");
            resp.getWriter().write("注册失败");
        }else{
            resp.getWriter().write("注册失败");
        }
        resp.setHeader("refresh","5;login.jsp");
    }
}
